package com.sztest.jayjun.utils;

import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnErrorListener;
import android.text.TextUtils;

import com.sztest.jayjun.MyApplication;
import com.sztest.jayjun.R;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

public class MediaManageUtils {

	private static MediaPlayer mMediaPlayer;   //播放录音文件
	private static boolean isPause;
	private static Listener mListener;
	private static Allistener sAllistener;

	public interface Listener{
		void error();
		void onPrepared();
	}
	public static void playSound(String filePath,Listener listener,MediaPlayer.OnCompletionListener onCompletionListenter){
		mListener=listener;
		if(mMediaPlayer==null){
			mMediaPlayer=new MediaPlayer();
			mMediaPlayer.setOnErrorListener( new OnErrorListener() {

				@Override
				public boolean onError(MediaPlayer mp, int what, int extra) {
					// TODO Auto-generated method stub
					mMediaPlayer.reset();
					mListener.error();
					return false;
				}
			});
			mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
				@Override
				public void onPrepared(MediaPlayer mp) {
					try {
						mp.start();
						mListener.onPrepared();
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			});
		}else{
			mMediaPlayer.reset();
		}
		try {
			mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
			mMediaPlayer.setOnCompletionListener(onCompletionListenter);
			mMediaPlayer.setDataSource(filePath);
			mMediaPlayer.prepareAsync();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 播放音频
	 * @param filePath
	 * @param onCompletionListenter
	 */
	public static void playSound(String filePath,MediaPlayer.OnCompletionListener onCompletionListenter){
		if(mMediaPlayer==null){
			mMediaPlayer=new MediaPlayer();
			mMediaPlayer.setOnErrorListener( new OnErrorListener() {

				@Override
				public boolean onError(MediaPlayer mp, int what, int extra) {
					// TODO Auto-generated method stub
					mMediaPlayer.reset();

					return false;
				}
			});
			mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
				@Override
				public void onPrepared(MediaPlayer mp) {
					try {
						mp.start();
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			});
		}else{
			mMediaPlayer.reset();
		}
		try {
			mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
			mMediaPlayer.setOnCompletionListener(onCompletionListenter);
			mMediaPlayer.setDataSource(filePath);
			mMediaPlayer.prepareAsync();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void playDi(MediaPlayer.OnCompletionListener onCompletionListenter){
		if(mMediaPlayer!=null){
			mMediaPlayer.reset();
		}
		mMediaPlayer = MediaPlayer.create(MyApplication.getInstance(), R.raw.didi_v2);
		mMediaPlayer.setOnErrorListener( new OnErrorListener() {

			@Override
			public boolean onError(MediaPlayer mp, int what, int extra) {
				// TODO Auto-generated method stub
				mMediaPlayer.reset();

				return false;
			}
		});
//		mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
//			@Override
//			public void onPrepared(MediaPlayer mp) {
//				try {
//					mp.start();
//				} catch (Exception e) {
//					e.printStackTrace();
//				}
//			}
//		});
		try {
			mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
			mMediaPlayer.setOnCompletionListener(onCompletionListenter);
//			mMediaPlayer.setDataSource(filePath);
			mMediaPlayer.start();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}


	public static void playSoundIAll(String filePath,Allistener allistener){
		sAllistener = allistener;
		if(mMediaPlayer==null){
			mMediaPlayer=new MediaPlayer();
			mMediaPlayer.setOnErrorListener( new OnErrorListener() {

				@Override
				public boolean onError(MediaPlayer mp, int what, int extra) {
					mMediaPlayer.reset();
					if (sAllistener!=null) {
						sAllistener.onError(mp, what, extra);
					}
					return false;
				}
			});
			mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
				@Override
				public void onPrepared(MediaPlayer mp) {
					try {
						mp.start();
					} catch (Exception e) {
						e.printStackTrace();
					}
					if (sAllistener!=null) {
						sAllistener.onPrepared(mp);
					}
				}
			});
		}else{
			mMediaPlayer.reset();
		}
		try {
			mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
			mMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
				@Override
				public void onCompletion(MediaPlayer mp) {
					if (sAllistener!=null) {
						sAllistener.onCompletion(mp);
					}
				}
			});
			mMediaPlayer.setDataSource(filePath);
			mMediaPlayer.prepareAsync();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 	暂停
	 */
	public static void pause(){
		if(mMediaPlayer!=null && mMediaPlayer.isPlaying()){
			mMediaPlayer.pause();
			isPause=true;
		}
	}
    public static boolean isPlaying() {
        if (mMediaPlayer != null) {
            return mMediaPlayer.isPlaying();
        } else {
            return false;
        }
    }

    /**
	 * resume重新开始
	 */
	public static void resume(){
		if(mMediaPlayer!=null && isPause){
			mMediaPlayer.start();
			isPause=false;
		}
	}

	/**
	 * release释放资源
	 */
	public static void release(){
		if(mMediaPlayer!=null){
			mMediaPlayer.stop();
			mMediaPlayer.release();
			mMediaPlayer=null;
		}
	}

	public static String formatEncode(String originUrl) {
		if (TextUtils.isEmpty(originUrl)) {
			return "";
		}
		String str = null;
		try {
			str = new String(originUrl.getBytes(), "UTF-8");
			str = URLEncoder.encode(str, "UTF-8");
			str = str.replace("%3A", ":");
			str = str.replace("%2F", "/");

			str = str.replace("%20", "+");
			str = str.replace("%3F", "?");
			str = str.replace("%25", "%");
			str = str.replace("%3D", "=");
			str = str.replace("%26", "&");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return str;
	}

	public static String formatEncode2(String originUrl) {
		if (TextUtils.isEmpty(originUrl)) {
			return "";
		}
		String str = null;
		try {
			str = new String(originUrl.getBytes(), "UTF-8");
			str = URLEncoder.encode(str, "UTF-8");
			str = str.replace("%3A", ":");
			str = str.replace("%2F", "/");

		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return str;
	}

	public interface  Allistener {
		boolean onError(MediaPlayer mp, int what, int extra);
		void onPrepared(MediaPlayer mp);
		void onCompletion(MediaPlayer mp);
	}


}
